# -*- coding: utf-8 -*-
 
import hashlib
from base import UserCase, Runner, TestSuite

class Case(UserCase):
    def create_device(self):
        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
        }
        res = self.c.open('/api/device/create', p)
        self.assertEqual(res['code'], 0)
        return res['data']

    def test_device_list(self):
        res = self.c.open('/api/device/list')
        self.assertEqual(res['code'], 0)

    def test_device_create(self):
        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
        }
        res = self.c.open('/api/device/create', p)
        self.assertEqual(res['code'], 0)

    def test_device_create2(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)

        corp_id = res['data']['id']

        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
            'corp': corp_id,
        }
        res = self.c.open('/api/device/create', p)
        self.assertEqual(res['code'], 0)


    def test_device_delete(self):
        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
        }
        res = self.c.open('/api/device/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        res = self.c.open('/api/device/list', {'orderBy': 'device.id', 'orderDesc': '1'})
        self.assertEqual(res['data']['itemList'][0]['id'], id)

        self.c.open('/api/device/delete', {'id': id})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/device/list')
        self.assertNotEqual(res['data']['itemList'][0]['id'], id)

    def test_device_update(self):
        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
        }
        res = self.c.open('/api/device/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        res = self.c.open('/api/device/read', {'id': id})
        self.assertEqual(res['data']['cpu_id'], p['cpu_id'])
        self.assertEqual(res['data']['name'], p['name'])

        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
            'id': id,
        }
        res = self.c.open('/api/device/update', p)
        self.assertEqual(res['code'], 0)
        res = self.c.open('/api/device/read', {'id': id})
        self.assertEqual(res['data']['cpu_id'], p['cpu_id'])
        self.assertEqual(res['data']['name'], p['name'])

    def test_device_read(self):
        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
        }
        res = self.c.open('/api/device/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        res = self.c.open('/api/device/read', {'id': id})
        self.assertEqual(res['data']['cpu_id'], p['cpu_id'])
        self.assertEqual(res['data']['name'], p['name'])


    def test_device_list_filter_by_corp(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        corp = res['data']['id']

        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
            'corp': corp,
        }
        res = self.c.open('/api/device/create', p)
        self.assertEqual(res['code'], 0)

        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
            'corp': corp,
        }
        res = self.c.open('/api/device/create', p)
        self.assertEqual(res['code'], 0)

        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
            'corp': corp,
        }
        res = self.c.open('/api/device/create', p)
        id = res['data']['id']
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/device/list', {'corp': corp, 'orderBy': 'id', 'orderDesc': '1'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 3)
        self.assertEqual(res['data']['itemList'][0]['id'], id)


    def test_device_list_filter_by_name(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        corp = res['data']['id']

        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
            'corp': corp,
        }
        res = self.c.open('/api/device/create', p)
        self.assertEqual(res['code'], 0)

        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
            'corp': corp,
        }
        res = self.c.open('/api/device/create', p)
        id = res['data']['id']
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/device/list', {'corp': corp, 'name': p['name'], 'orderBy': 'create', 'orderDesc': '1'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 1)
        self.assertEqual(res['data']['itemList'][0]['id'], id)


    def test_device_list_filter_by_id(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        corp = res['data']['id']

        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
            'corp': corp,
        }
        res = self.c.open('/api/device/create', p)
        self.assertEqual(res['code'], 0)

        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
            'corp': corp,
        }
        res = self.c.open('/api/device/create', p)
        id = res['data']['id']
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/device/list', {'corp': corp, 'id': id, 'orderBy': 'create', 'orderDesc': '1'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 1)
        self.assertEqual(res['data']['itemList'][0]['id'], id)


    def test_device_list_filter_by_cpu_id(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        corp = res['data']['id']

        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
            'corp': corp,
        }
        res = self.c.open('/api/device/create', p)
        self.assertEqual(res['code'], 0)

        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
            'corp': corp,
        }
        res = self.c.open('/api/device/create', p)
        id = res['data']['id']
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/device/list', {'corp': corp, 'cpu_id': p['cpu_id'], 'orderBy': 'create', 'orderDesc': '1'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 1)
        self.assertEqual(res['data']['itemList'][0]['id'], id)


    def test_device_delete_corp_device(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        corp = res['data']['id']

        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
            'corp': corp,
        }
        res = self.c.open('/api/device/create', p)
        self.assertEqual(res['code'], 0)
        device = res['data']['id']

        res = self.c.open('/api/corp-device/list', {'corp': corp})
        self.assertEqual(res['data']['count'], 1)

        res = self.c.open('/api/device/delete', {'id': device})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/corp-device/list', {'corp': corp})
        self.assertEqual(res['data']['count'], 0)


    def test_device_field_alias(self):

        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
        }
        res = self.c.open('/api/device/create', p)
        self.assertEqual(res['code'], 0)
        
        res = self.c.open('/api/device/field-alias', {'cpu_id': p['cpu_id']})
        self.assertEqual(res['code'], 0)


    def test_device_field_alias_update(self):
        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
        }
        res = self.c.open('/api/device/create', p)
        self.assertEqual(res['code'], 0)
        
        res = self.c.open('/api/device/field-alias', {'cpu_id': p['cpu_id']})
        self.assertEqual(res['code'], 0)

        map = res['data']['map']
        for k in map:
            map[k] = self.randhan(8)

        map['cpu_id'] = p['cpu_id']
        res = self.c.open('/api/device/field-alias-update', map)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/device/field-alias', {'cpu_id': p['cpu_id']})
        self.assertEqual(res['code'], 0)
        nmap = res['data']['map']

        keys = nmap.keys()
        keys.sort()
        s1 = '|'.join('%s%s' % (k, nmap[k]) for k in keys)
        s2 = '|'.join('%s%s' % (k, map[k]) for k in keys)
        self.assertEqual(hashlib.md5(s1.encode('utf8')).hexdigest(),
                         hashlib.md5(s2.encode('utf8')).hexdigest())


    def test_device_apply_rule(self):
        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
        }
        res = self.c.open('/api/device/create', p)
        self.assertEqual(res['code'], 0)

        device_id = res['data']['id']

        p = {
            'name': self.randhan(8),
            'code': self.randchar(8),
            'field': self.randchar(12),
            'op': self.randchar(3),
            'value': self.randnum(3),
        }
        res = self.c.open('/api/rule/create', p)
        self.assertEqual(res['code'], 0)

        rule_id = res['data']['id']

        p = {
            'name': self.randhan(8),
            'code': self.randchar(8),
            'field': self.randchar(12),
            'op': self.randchar(3),
            'value': self.randnum(3),
        }
        res = self.c.open('/api/rule/create', p)
        self.assertEqual(res['code'], 0)

        rule_id2 = res['data']['id']

        p = {
            'id': device_id,
            'rule': ','.join([str(rule_id), str(rule_id2)])
        }
        res = self.c.open('/api/device/apply-rule', p)
        self.assertEqual(res['code'], 0)


    def test_device_get_rule(self):
        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
        }
        res = self.c.open('/api/device/create', p)
        self.assertEqual(res['code'], 0)

        device_id = res['data']['id']

        p = {
            'name': self.randhan(8),
            'code': self.randchar(8),
            'field': self.randchar(12),
            'op': self.randchar(3),
            'value': self.randnum(3),
        }
        res = self.c.open('/api/rule/create', p)
        self.assertEqual(res['code'], 0)

        rule_id = res['data']['id']

        p = {
            'name': self.randhan(8),
            'code': self.randchar(8),
            'field': self.randchar(12),
            'op': self.randchar(3),
            'value': self.randnum(3),
        }
        res = self.c.open('/api/rule/create', p)
        self.assertEqual(res['code'], 0)

        rule_id2 = res['data']['id']

        p = {
            'id': device_id,
            'rule': ','.join([str(rule_id), str(rule_id2)])
        }
        res = self.c.open('/api/device/apply-rule', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/device/get-rule', {'id': device_id})
        self.assertEqual(res['code'], 0)

        a = set(x['id'] for x in res['data'])
        b = set([rule_id, rule_id2])
        self.assertEqual(a, b)

    def test_device_set_attribute(self):
        device = self.create_device()
        p = {
            'cpu_id': device['cpu_id'],
            'code': self.randchar(8),
            'name': self.randhan(8),
            'value': self.randchar(64),
        }
        res = self.c.open('/api/device/set-attribute', p)
        self.assertEqual(res['code'], 0)

    def test_device_get_attribute_list(self):
        device = self.create_device()
        p = {
            'cpu_id': device['cpu_id'],
            'code': self.randchar(8),
            'name': self.randhan(8),
            'value': self.randchar(64),
        }
        res = self.c.open('/api/device/set-attribute', p)
        self.assertEqual(res['code'], 0)

        p = {
            'cpu_id': device['cpu_id'],
            'code': self.randchar(8),
            'name': self.randhan(8),
            'value': self.randchar(64),
        }
        res = self.c.open('/api/device/set-attribute', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/device/get-attribute-list', {'cpu_id': device['cpu_id']})
        self.assertEqual(res['code'], 0)
        self.assertEqual(len(res['data']), 2)


    def test_device_get_attribute_list_filter(self):
        device = self.create_device()
        p = {
            'cpu_id': device['cpu_id'],
            'code': self.randchar(8),
            'name': self.randhan(8),
            'value': self.randchar(64),
        }
        res = self.c.open('/api/device/set-attribute', p)
        self.assertEqual(res['code'], 0)

        p = {
            'cpu_id': device['cpu_id'],
            'code': self.randchar(8),
            'name': self.randhan(8),
            'value': self.randchar(64),
        }
        res = self.c.open('/api/device/set-attribute', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/device/get-attribute-list', {'cpu_id': device['cpu_id'], 'code': p['code']})
        self.assertEqual(res['code'], 0)
        self.assertEqual(len(res['data']), 1)


    def test_device_delete_attribute(self):
        device = self.create_device()
        p = {
            'cpu_id': device['cpu_id'],
            'code': self.randchar(8),
            'name': self.randhan(8),
            'value': self.randchar(64),
        }
        res = self.c.open('/api/device/set-attribute', p)
        self.assertEqual(res['code'], 0)

        p = {
            'cpu_id': device['cpu_id'],
            'code': self.randchar(8),
            'name': self.randhan(8),
            'value': self.randchar(64),
        }
        res = self.c.open('/api/device/set-attribute', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/device/get-attribute-list', {'cpu_id': device['cpu_id']})
        self.assertEqual(res['code'], 0)
        self.assertEqual(len(res['data']), 2)

        res = self.c.open('/api/device/delete-attribute', {'cpu_id': device['cpu_id'], 'code': p['code']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/device/get-attribute-list', {'cpu_id': device['cpu_id']})
        self.assertEqual(res['code'], 0)
        self.assertEqual(len(res['data']), 1)



test_list = [
    #'test_device_list',
    #'test_device_create',
    #'test_device_create2',
    #'test_device_delete',
    #'test_device_update',
    #'test_device_read',
    #'test_device_list_filter_by_corp',
    #'test_device_list_filter_by_name',
    #'test_device_list_filter_by_id',
    #'test_device_list_filter_by_cpu_id',
    #'test_device_delete_corp_device',
    #'test_device_field_alias',
    #'test_device_field_alias_update',
    #'test_device_apply_rule',
    #'test_device_get_rule',
    #'test_device_set_attribute',
    #'test_device_get_attribute_list',
    'test_device_get_attribute_list_filter',
    #'test_device_delete_attribute',
]
Suite = TestSuite([Case(t) for t in test_list])


if __name__ == '__main__':
    Runner.run(Suite)



